package com.erps.sys.rest;

import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.StringEx;
import com.erps.db.DBManager;
import com.hmkj.entity.ErspResOnline;
import com.hmkj.entity.vo.ersp_res_online_vo;

public class ErspResOnlineRest {
	public PageData Query(int iPageNO, int iPageSize, String cSort, String cShowType, String cProZone, String cKeyWord) {
		String cWhereParm = "";
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) ");
		sql.append(" from ersp_res_info x,ersp_res_online y ");
		sql.append(" where x.id=y.id ");
		sql.append(" and x.res_release=1 ");

		if (StringEx.getStr(cShowType).length() > 1) {
			sql.append(cWhereParm.length() > 0 ? " and res_proname='" + cProZone + "'" : " res_proname='" + cProZone + "'");
		}
		if (StringEx.getStr(cProZone).length() > 1) {
			sql.append(cWhereParm.length() > 0 ? " and res_prozone='" + cProZone + "'" : " res_prozone='" + cProZone + "'");
		}

		if (StringEx.getStr(cKeyWord).length() > 1) {
			sql.append(cWhereParm.length() > 0 ? " and res_title like '%" + cProZone + "%'" : " res_prozone like '%" + cKeyWord + "%'");
		}

		// 添加Where条件
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(cWhereParm);
		}
		String cOrderBy;

		if (cSort.equals("1")) {
			cOrderBy = " order by x.res_order_count desc ";
		} else if (cSort.equals("2")) {
			cOrderBy = " order by x.last_update_time desc ";
		} else {
			cOrderBy = " order by x.res_view_count desc ";
		}

		int iRowCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));
		int iPageCount = (int) Math.ceil(iRowCount / (double) iPageSize);
		if (iPageNO < 0)
			iPageNO = 1;
		else if (iPageNO > iPageCount)
			iPageNO = iPageCount;

		sql.setLength(0);
		sql.append(" select x.id,x.org_id,x.res_title,x.res_desc,res_summy,res_prozone, res_contact,res_contact_phone,y.res_prozone,y.res_onlinetype,y.res_viewtype,y.res_size");
		sql.append(" from ersp_res_info x,ersp_res_online y ");
		sql.append(" where x.id=y.id ");
		sql.append(" and x.res_release=1 ");

		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString(), cOrderBy, iPageNO, iPageSize);
		PageData ret = new PageData();
		ret.setRowcount(iRowCount);
		ret.setPagecount(iPageCount);
		ret.setPageno(iPageNO);

		int iLength = (dtRows == null) ? 0 : dtRows.size();
		for (int i = 0; i < iLength; i++) {
			Map<String, String> vo = dtRows.get(i);
			ersp_res_online_vo voRes = new ersp_res_online_vo();
			voRes.setId(StringEx.getMapVal(vo, "id"));
			voRes.setRes_id(StringEx.getMapVal(vo, "res_id"));
			voRes.setRes_desc(StringEx.getMapVal(vo, "res_desc"));
			voRes.setRes_summy(StringEx.getMapVal(vo, "res_summy"));
			voRes.setRes_prozone(StringEx.getMapVal(vo, "res_prozone"));
			voRes.setRes_viewtype(StringEx.getMapVal(vo, "res_viewtype"));

			voRes.setOrg_name(new ErspSysOrgRest().FindOne(voRes.getId()).getOrg_name());

			ret.getKeylist().add(voRes);
		}
		return ret;
	}

	public ErspResOnline FindOne(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_res_online ");
		sql.append(" where id='" + cKeyID + "'");
		ErspResOnline vo = new ErspResOnline();

		Map<String, String> rs = DBManager.GetRow(sql.toString());
		vo.setId(StringEx.getMapVal(rs, "id"));
		vo.setRes_id(StringEx.getMapVal(rs, "res_id"));

		vo.setId(StringEx.getMapVal(rs, "id"));
		vo.setRes_id(StringEx.getMapVal(rs, "res_id"));
		vo.setRes_prozone(StringEx.getMapVal(rs, "res_prozone"));
		vo.setRes_onlinetype(StringEx.getMapVal(rs, "res_onlinetype"));
		vo.setRes_viewtype(StringEx.getMapVal(rs, "res_viewtype"));
		vo.setRes_size(StringEx.getMapVal(rs, "res_size"));
		vo.setRes_language(StringEx.getMapVal(rs, "res_language"));
		vo.setRes_copyright(StringEx.getMapVal(rs, "res_copyright"));
		vo.setRes_request_type(StringEx.getMapVal(rs, "res_request_type"));
		vo.setRes_request_url(StringEx.getMapVal(rs, "res_request_url"));
		vo.setRes_requirement(StringEx.getMapVal(rs, "res_requirement"));
		vo.setLast_update_time(Long.parseLong(StringEx.getMapVal(rs, "last_update_time")));

		return vo;

	}
}